home *** CD-ROM | disk | FTP | other *** search
/ SGI Freeware 1998 November / Freeware November 1998.img / dist / fw_elisp-manual-19.idb / usr / freeware / info / elisp-5.z / elisp-5 (.txt)
GNU Info File  |  1998-05-26  |  45KB  |  927 lines

  1. This is Info file elisp, produced by Makeinfo-1.63 from the input file
  2. elisp.texi.
  3.    This version is the edition 2.4.2 of the GNU Emacs Lisp Reference
  4. Manual.  It corresponds to Emacs Version 19.34.
  5.    Published by the Free Software Foundation 59 Temple Place, Suite 330
  6. Boston, MA  02111-1307  USA
  7.    Copyright (C) 1990, 1991, 1992, 1993, 1994, 1995, 1996 Free Software
  8. Foundation, Inc.
  9.    Permission is granted to make and distribute verbatim copies of this
  10. manual provided the copyright notice and this permission notice are
  11. preserved on all copies.
  12.    Permission is granted to copy and distribute modified versions of
  13. this manual under the conditions for verbatim copying, provided that the
  14. entire resulting derived work is distributed under the terms of a
  15. permission notice identical to this one.
  16.    Permission is granted to copy and distribute translations of this
  17. manual into another language, under the above conditions for modified
  18. versions, except that this permission notice may be stated in a
  19. translation approved by the Foundation.
  20.    Permission is granted to copy and distribute modified versions of
  21. this manual under the conditions for verbatim copying, provided also
  22. that the section entitled "GNU General Public License" is included
  23. exactly as in the original, and provided that the entire resulting
  24. derived work is distributed under the terms of a permission notice
  25. identical to this one.
  26.    Permission is granted to copy and distribute translations of this
  27. manual into another language, under the above conditions for modified
  28. versions, except that the section entitled "GNU General Public License"
  29. may be included in a translation approved by the Free Software
  30. Foundation instead of in the original English.
  31. File: elisp,  Node: Formatting Strings,  Next: Character Case,  Prev: String Conversion,  Up: Strings and Characters
  32. Formatting Strings
  33. ==================
  34.    "Formatting" means constructing a string by substitution of computed
  35. values at various places in a constant string.  This string controls
  36. how the other values are printed as well as where they appear; it is
  37. called a "format string".
  38.    Formatting is often useful for computing messages to be displayed.
  39. In fact, the functions `message' and `error' provide the same
  40. formatting feature described here; they differ from `format' only in
  41. how they use the result of formatting.
  42.  - Function: format STRING &rest OBJECTS
  43.      This function returns a new string that is made by copying STRING
  44.      and then replacing any format specification in the copy with
  45.      encodings of the corresponding OBJECTS.  The arguments OBJECTS are
  46.      the computed values to be formatted.
  47.    A format specification is a sequence of characters beginning with a
  48. `%'.  Thus, if there is a `%d' in STRING, the `format' function
  49. replaces it with the printed representation of one of the values to be
  50. formatted (one of the arguments OBJECTS).  For example:
  51.      (format "The value of fill-column is %d." fill-column)
  52.           => "The value of fill-column is 72."
  53.    If STRING contains more than one format specification, the format
  54. specifications correspond with successive values from OBJECTS.  Thus,
  55. the first format specification in STRING uses the first such value, the
  56. second format specification uses the second such value, and so on.  Any
  57. extra format specifications (those for which there are no corresponding
  58. values) cause unpredictable behavior.  Any extra values to be formatted
  59. are ignored.
  60.    Certain format specifications require values of particular types.
  61. However, no error is signaled if the value actually supplied fails to
  62. have the expected type.  Instead, the output is likely to be
  63. meaningless.
  64.    Here is a table of valid format specifications:
  65.      Replace the specification with the printed representation of the
  66.      object, made without quoting.  Thus, strings are represented by
  67.      their contents alone, with no `"' characters, and symbols appear
  68.      without `\' characters.
  69.      If there is no corresponding object, the empty string is used.
  70.      Replace the specification with the printed representation of the
  71.      object, made with quoting.  Thus, strings are enclosed in `"'
  72.      characters, and `\' characters appear where necessary before
  73.      special characters.
  74.      If there is no corresponding object, the empty string is used.
  75.      Replace the specification with the base-eight representation of an
  76.      integer.
  77.      Replace the specification with the base-ten representation of an
  78.      integer.
  79.      Replace the specification with the base-sixteen representation of
  80.      an integer.
  81.      Replace the specification with the character which is the value
  82.      given.
  83.      Replace the specification with the exponential notation for a
  84.      floating point number.
  85.      Replace the specification with the decimal-point notation for a
  86.      floating point number.
  87.      Replace the specification with notation for a floating point
  88.      number, using either exponential notation or decimal-point
  89.      notation whichever is shorter.
  90.      A single `%' is placed in the string.  This format specification is
  91.      unusual in that it does not use a value.  For example, `(format "%%
  92.      %d" 30)' returns `"% 30"'.
  93.    Any other format character results in an `Invalid format operation'
  94. error.
  95.    Here are several examples:
  96.      (format "The name of this buffer is %s." (buffer-name))
  97.           => "The name of this buffer is strings.texi."
  98.      
  99.      (format "The buffer object prints as %s." (current-buffer))
  100.           => "The buffer object prints as strings.texi."
  101.      
  102.      (format "The octal value of %d is %o,
  103.               and the hex value is %x." 18 18 18)
  104.           => "The octal value of 18 is 22,
  105.               and the hex value is 12."
  106.    All the specification characters allow an optional numeric prefix
  107. between the `%' and the character.  The optional numeric prefix defines
  108. the minimum width for the object.  If the printed representation of the
  109. object contains fewer characters than this, then it is padded.  The
  110. padding is on the left if the prefix is positive (or starts with zero)
  111. and on the right if the prefix is negative.  The padding character is
  112. normally a space, but if the numeric prefix starts with a zero, zeros
  113. are used for padding.
  114.      (format "%06d is padded on the left with zeros" 123)
  115.           => "000123 is padded on the left with zeros"
  116.      
  117.      (format "%-6d is padded on the right" 123)
  118.           => "123    is padded on the right"
  119.    `format' never truncates an object's printed representation, no
  120. matter what width you specify.  Thus, you can use a numeric prefix to
  121. specify a minimum spacing between columns with no risk of losing
  122. information.
  123.    In the following three examples, `%7s' specifies a minimum width of
  124. 7.  In the first case, the string inserted in place of `%7s' has only 3
  125. letters, so 4 blank spaces are inserted for padding.  In the second
  126. case, the string `"specification"' is 13 letters wide but is not
  127. truncated.  In the third case, the padding is on the right.
  128.      (format "The word `%7s' actually has %d letters in it."
  129.              "foo" (length "foo"))
  130.           => "The word `    foo' actually has 3 letters in it."
  131.      (format "The word `%7s' actually has %d letters in it."
  132.              "specification" (length "specification"))
  133.           => "The word `specification' actually has 13 letters in it."
  134.      (format "The word `%-7s' actually has %d letters in it."
  135.              "foo" (length "foo"))
  136.           => "The word `foo    ' actually has 3 letters in it."
  137. File: elisp,  Node: Character Case,  Next: Case Table,  Prev: Formatting Strings,  Up: Strings and Characters
  138. Character Case
  139. ==============
  140.    The character case functions change the case of single characters or
  141. of the contents of strings.  The functions convert only alphabetic
  142. characters (the letters `A' through `Z' and `a' through `z'); other
  143. characters are not altered.  The functions do not modify the strings
  144. that are passed to them as arguments.
  145.    The examples below use the characters `X' and `x' which have ASCII
  146. codes 88 and 120 respectively.
  147.  - Function: downcase STRING-OR-CHAR
  148.      This function converts a character or a string to lower case.
  149.      When the argument to `downcase' is a string, the function creates
  150.      and returns a new string in which each letter in the argument that
  151.      is upper case is converted to lower case.  When the argument to
  152.      `downcase' is a character, `downcase' returns the corresponding
  153.      lower case character.  This value is an integer.  If the original
  154.      character is lower case, or is not a letter, then the value equals
  155.      the original character.
  156.           (downcase "The cat in the hat")
  157.                => "the cat in the hat"
  158.           
  159.           (downcase ?X)
  160.                => 120
  161.  - Function: upcase STRING-OR-CHAR
  162.      This function converts a character or a string to upper case.
  163.      When the argument to `upcase' is a string, the function creates
  164.      and returns a new string in which each letter in the argument that
  165.      is lower case is converted to upper case.
  166.      When the argument to `upcase' is a character, `upcase' returns the
  167.      corresponding upper case character.  This value is an integer.  If
  168.      the original character is upper case, or is not a letter, then the
  169.      value equals the original character.
  170.           (upcase "The cat in the hat")
  171.                => "THE CAT IN THE HAT"
  172.           
  173.           (upcase ?x)
  174.                => 88
  175.  - Function: capitalize STRING-OR-CHAR
  176.      This function capitalizes strings or characters.  If
  177.      STRING-OR-CHAR is a string, the function creates and returns a new
  178.      string, whose contents are a copy of STRING-OR-CHAR in which each
  179.      word has been capitalized.  This means that the first character of
  180.      each word is converted to upper case, and the rest are converted
  181.      to lower case.
  182.      The definition of a word is any sequence of consecutive characters
  183.      that are assigned to the word constituent syntax class in the
  184.      current syntax table (*Note Syntax Class Table::).
  185.      When the argument to `capitalize' is a character, `capitalize' has
  186.      the same result as `upcase'.
  187.           (capitalize "The cat in the hat")
  188.                => "The Cat In The Hat"
  189.           
  190.           (capitalize "THE 77TH-HATTED CAT")
  191.                => "The 77th-Hatted Cat"
  192.           
  193.           (capitalize ?x)
  194.                => 88
  195. File: elisp,  Node: Case Table,  Prev: Character Case,  Up: Strings and Characters
  196. The Case Table
  197. ==============
  198.    You can customize case conversion by installing a special "case
  199. table".  A case table specifies the mapping between upper case and lower
  200. case letters.  It affects both the string and character case conversion
  201. functions (see the previous section) and those that apply to text in the
  202. buffer (*note Case Changes::.).  You need a case table if you are using
  203. a language which has letters other than the standard ASCII letters.
  204.    A case table is a list of this form:
  205.      (DOWNCASE UPCASE CANONICALIZE EQUIVALENCES)
  206. where each element is either `nil' or a string of length 256.  The
  207. element DOWNCASE says how to map each character to its lower-case
  208. equivalent.  The element UPCASE maps each character to its upper-case
  209. equivalent.  If lower and upper case characters are in one-to-one
  210. correspondence, use `nil' for UPCASE; then Emacs deduces the upcase
  211. table from DOWNCASE.
  212.    For some languages, upper and lower case letters are not in
  213. one-to-one correspondence.  There may be two different lower case
  214. letters with the same upper case equivalent.  In these cases, you need
  215. to specify the maps for both directions.
  216.    The element CANONICALIZE maps each character to a canonical
  217. equivalent; any two characters that are related by case-conversion have
  218. the same canonical equivalent character.
  219.    The element EQUIVALENCES is a map that cyclicly permutes each
  220. equivalence class (of characters with the same canonical equivalent).
  221. (For ordinary ASCII, this would map `a' into `A' and `A' into `a', and
  222. likewise for each set of equivalent characters.)
  223.    When you construct a case table, you can provide `nil' for
  224. CANONICALIZE; then Emacs fills in this string from UPCASE and DOWNCASE.
  225. You can also provide `nil' for EQUIVALENCES; then Emacs fills in this
  226. string from CANONICALIZE.  In a case table that is actually in use,
  227. those components are non-`nil'.  Do not try to specify EQUIVALENCES
  228. without also specifying CANONICALIZE.
  229.    Each buffer has a case table.  Emacs also has a "standard case
  230. table" which is copied into each buffer when you create the buffer.
  231. Changing the standard case table doesn't affect any existing buffers.
  232.    Here are the functions for working with case tables:
  233.  - Function: case-table-p OBJECT
  234.      This predicate returns non-`nil' if OBJECT is a valid case table.
  235.  - Function: set-standard-case-table TABLE
  236.      This function makes TABLE the standard case table, so that it will
  237.      apply to any buffers created subsequently.
  238.  - Function: standard-case-table
  239.      This returns the standard case table.
  240.  - Function: current-case-table
  241.      This function returns the current buffer's case table.
  242.  - Function: set-case-table TABLE
  243.      This sets the current buffer's case table to TABLE.
  244.    The following three functions are convenient subroutines for packages
  245. that define non-ASCII character sets.  They modify a string
  246. DOWNCASE-TABLE provided as an argument; this should be a string to be
  247. used as the DOWNCASE part of a case table.  They also modify the
  248. standard syntax table.  *Note Syntax Tables::.
  249.  - Function: set-case-syntax-pair UC LC DOWNCASE-TABLE
  250.      This function specifies a pair of corresponding letters, one upper
  251.      case and one lower case.
  252.  - Function: set-case-syntax-delims L R DOWNCASE-TABLE
  253.      This function makes characters L and R a matching pair of
  254.      case-invariant delimiters.
  255.  - Function: set-case-syntax CHAR SYNTAX DOWNCASE-TABLE
  256.      This function makes CHAR case-invariant, with syntax SYNTAX.
  257.  - Command: describe-buffer-case-table
  258.      This command displays a description of the contents of the current
  259.      buffer's case table.
  260.    You can load the library `iso-syntax' to set up the standard syntax
  261. table and define a case table for the 8-bit ISO Latin 1 character set.
  262. File: elisp,  Node: Lists,  Next: Sequences Arrays Vectors,  Prev: Strings and Characters,  Up: Top
  263. Lists
  264. *****
  265.    A "list" represents a sequence of zero or more elements (which may
  266. be any Lisp objects).  The important difference between lists and
  267. vectors is that two or more lists can share part of their structure; in
  268. addition, you can insert or delete elements in a list without copying
  269. the whole list.
  270. * Menu:
  271. * Cons Cells::          How lists are made out of cons cells.
  272. * Lists as Boxes::                 Graphical notation to explain lists.
  273. * List-related Predicates::        Is this object a list?  Comparing two lists.
  274. * List Elements::       Extracting the pieces of a list.
  275. * Building Lists::      Creating list structure.
  276. * Modifying Lists::     Storing new pieces into an existing list.
  277. * Sets And Lists::      A list can represent a finite mathematical set.
  278. * Association Lists::   A list can represent a finite relation or mapping.
  279. File: elisp,  Node: Cons Cells,  Next: Lists as Boxes,  Up: Lists
  280. Lists and Cons Cells
  281. ====================
  282.    Lists in Lisp are not a primitive data type; they are built up from
  283. "cons cells".  A cons cell is a data object that represents an ordered
  284. pair.  It records two Lisp objects, one labeled as the CAR, and the
  285. other labeled as the CDR.  These names are traditional; see *Note Cons
  286. Cell Type::.  CDR is pronounced "could-er."
  287.    A list is a series of cons cells chained together, one cons cell per
  288. element of the list.  By convention, the CARs of the cons cells are the
  289. elements of the list, and the CDRs are used to chain the list: the CDR
  290. of each cons cell is the following cons cell.  The CDR of the last cons
  291. cell is `nil'.  This asymmetry between the CAR and the CDR is entirely
  292. a matter of convention; at the level of cons cells, the CAR and CDR
  293. slots have the same characteristics.
  294.    Because most cons cells are used as part of lists, the phrase "list
  295. structure" has come to mean any structure made out of cons cells.
  296.    The symbol `nil' is considered a list as well as a symbol; it is the
  297. list with no elements.  For convenience, the symbol `nil' is considered
  298. to have `nil' as its CDR (and also as its CAR).
  299.    The CDR of any nonempty list L is a list containing all the elements
  300. of L except the first.
  301. File: elisp,  Node: Lists as Boxes,  Next: List-related Predicates,  Prev: Cons Cells,  Up: Lists
  302. Lists as Linked Pairs of Boxes
  303. ==============================
  304.    A cons cell can be illustrated as a pair of boxes.  The first box
  305. represents the CAR and the second box represents the CDR.  Here is an
  306. illustration of the two-element list, `(tulip lily)', made from two
  307. cons cells:
  308.       ---------------         ---------------
  309.      | car   | cdr   |       | car   | cdr   |
  310.      | tulip |   o---------->| lily  |  nil  |
  311.      |       |       |       |       |       |
  312.       ---------------         ---------------
  313.    Each pair of boxes represents a cons cell.  Each box "refers to",
  314. "points to" or "contains" a Lisp object.  (These terms are synonymous.)
  315. The first box, which is the CAR of the first cons cell, contains the
  316. symbol `tulip'.  The arrow from the CDR of the first cons cell to the
  317. second cons cell indicates that the CDR of the first cons cell points
  318. to the second cons cell.
  319.    The same list can be illustrated in a different sort of box notation
  320. like this:
  321.          ___ ___      ___ ___
  322.         |___|___|--> |___|___|--> nil
  323.           |            |
  324.           |            |
  325.            --> tulip    --> lily
  326.    Here is a more complex illustration, showing the three-element list,
  327. `((pine needles) oak maple)', the first element of which is a
  328. two-element list:
  329.          ___ ___      ___ ___      ___ ___
  330.         |___|___|--> |___|___|--> |___|___|--> nil
  331.           |            |            |
  332.           |            |            |
  333.           |             --> oak      --> maple
  334.           |
  335.           |     ___ ___      ___ ___
  336.            --> |___|___|--> |___|___|--> nil
  337.                  |            |
  338.                  |            |
  339.                   --> pine     --> needles
  340.    The same list represented in the first box notation looks like this:
  341.       --------------       --------------       --------------
  342.      | car   | cdr  |     | car   | cdr  |     | car   | cdr  |
  343.      |   o   |   o------->| oak   |   o------->| maple |  nil |
  344.      |   |   |      |     |       |      |     |       |      |
  345.       -- | ---------       --------------       --------------
  346.          |
  347.          |
  348.          |        --------------       ----------------
  349.          |       | car   | cdr  |     | car     | cdr  |
  350.           ------>| pine  |   o------->| needles |  nil |
  351.                  |       |      |     |         |      |
  352.                   --------------       ----------------
  353.    *Note Cons Cell Type::, for the read and print syntax of cons cells
  354. and lists, and for more "box and arrow" illustrations of lists.
  355. File: elisp,  Node: List-related Predicates,  Next: List Elements,  Prev: Lists as Boxes,  Up: Lists
  356. Predicates on Lists
  357. ===================
  358.    The following predicates test whether a Lisp object is an atom, is a
  359. cons cell or is a list, or whether it is the distinguished object
  360. `nil'.  (Many of these predicates can be defined in terms of the
  361. others, but they are used so often that it is worth having all of them.)
  362.  - Function: consp OBJECT
  363.      This function returns `t' if OBJECT is a cons cell, `nil'
  364.      otherwise.  `nil' is not a cons cell, although it *is* a list.
  365.  - Function: atom OBJECT
  366.      This function returns `t' if OBJECT is an atom, `nil' otherwise.
  367.      All objects except cons cells are atoms.  The symbol `nil' is an
  368.      atom and is also a list; it is the only Lisp object that is both.
  369.           (atom OBJECT) == (not (consp OBJECT))
  370.  - Function: listp OBJECT
  371.      This function returns `t' if OBJECT is a cons cell or `nil'.
  372.      Otherwise, it returns `nil'.
  373.           (listp '(1))
  374.                => t
  375.           (listp '())
  376.                => t
  377.  - Function: nlistp OBJECT
  378.      This function is the opposite of `listp': it returns `t' if OBJECT
  379.      is not a list.  Otherwise, it returns `nil'.
  380.           (listp OBJECT) == (not (nlistp OBJECT))
  381.  - Function: null OBJECT
  382.      This function returns `t' if OBJECT is `nil', and returns `nil'
  383.      otherwise.  This function is identical to `not', but as a matter
  384.      of clarity we use `null' when OBJECT is considered a list and
  385.      `not' when it is considered a truth value (see `not' in *Note
  386.      Combining Conditions::).
  387.           (null '(1))
  388.                => nil
  389.           (null '())
  390.                => t
  391. File: elisp,  Node: List Elements,  Next: Building Lists,  Prev: List-related Predicates,  Up: Lists
  392. Accessing Elements of Lists
  393. ===========================
  394.  - Function: car CONS-CELL
  395.      This function returns the value pointed to by the first pointer of
  396.      the cons cell CONS-CELL.  Expressed another way, this function
  397.      returns the CAR of CONS-CELL.
  398.      As a special case, if CONS-CELL is `nil', then `car' is defined to
  399.      return `nil'; therefore, any list is a valid argument for `car'.
  400.      An error is signaled if the argument is not a cons cell or `nil'.
  401.           (car '(a b c))
  402.                => a
  403.           (car '())
  404.                => nil
  405.  - Function: cdr CONS-CELL
  406.      This function returns the value pointed to by the second pointer of
  407.      the cons cell CONS-CELL.  Expressed another way, this function
  408.      returns the CDR of CONS-CELL.
  409.      As a special case, if CONS-CELL is `nil', then `cdr' is defined to
  410.      return `nil'; therefore, any list is a valid argument for `cdr'.
  411.      An error is signaled if the argument is not a cons cell or `nil'.
  412.           (cdr '(a b c))
  413.                => (b c)
  414.           (cdr '())
  415.                => nil
  416.  - Function: car-safe OBJECT
  417.      This function lets you take the CAR of a cons cell while avoiding
  418.      errors for other data types.  It returns the CAR of OBJECT if
  419.      OBJECT is a cons cell, `nil' otherwise.  This is in contrast to
  420.      `car', which signals an error if OBJECT is not a list.
  421.           (car-safe OBJECT)
  422.           ==
  423.           (let ((x OBJECT))
  424.             (if (consp x)
  425.                 (car x)
  426.               nil))
  427.  - Function: cdr-safe OBJECT
  428.      This function lets you take the CDR of a cons cell while avoiding
  429.      errors for other data types.  It returns the CDR of OBJECT if
  430.      OBJECT is a cons cell, `nil' otherwise.  This is in contrast to
  431.      `cdr', which signals an error if OBJECT is not a list.
  432.           (cdr-safe OBJECT)
  433.           ==
  434.           (let ((x OBJECT))
  435.             (if (consp x)
  436.                 (cdr x)
  437.               nil))
  438.  - Function: nth N LIST
  439.      This function returns the Nth element of LIST.  Elements are
  440.      numbered starting with zero, so the CAR of LIST is element number
  441.      zero.  If the length of LIST is N or less, the value is `nil'.
  442.      If N is negative, `nth' returns the first element of LIST.
  443.           (nth 2 '(1 2 3 4))
  444.                => 3
  445.           (nth 10 '(1 2 3 4))
  446.                => nil
  447.           (nth -3 '(1 2 3 4))
  448.                => 1
  449.           
  450.           (nth n x) == (car (nthcdr n x))
  451.  - Function: nthcdr N LIST
  452.      This function returns the Nth CDR of LIST.  In other words, it
  453.      removes the first N links of LIST and returns what follows.
  454.      If N is zero or negative, `nthcdr' returns all of LIST.  If the
  455.      length of LIST is N or less, `nthcdr' returns `nil'.
  456.           (nthcdr 1 '(1 2 3 4))
  457.                => (2 3 4)
  458.           (nthcdr 10 '(1 2 3 4))
  459.                => nil
  460.           (nthcdr -3 '(1 2 3 4))
  461.                => (1 2 3 4)
  462. File: elisp,  Node: Building Lists,  Next: Modifying Lists,  Prev: List Elements,  Up: Lists
  463. Building Cons Cells and Lists
  464. =============================
  465.    Many functions build lists, as lists reside at the very heart of
  466. Lisp.  `cons' is the fundamental list-building function; however, it is
  467. interesting to note that `list' is used more times in the source code
  468. for Emacs than `cons'.
  469.  - Function: cons OBJECT1 OBJECT2
  470.      This function is the fundamental function used to build new list
  471.      structure.  It creates a new cons cell, making OBJECT1 the CAR,
  472.      and OBJECT2 the CDR.  It then returns the new cons cell.  The
  473.      arguments OBJECT1 and OBJECT2 may be any Lisp objects, but most
  474.      often OBJECT2 is a list.
  475.           (cons 1 '(2))
  476.                => (1 2)
  477.           (cons 1 '())
  478.                => (1)
  479.           (cons 1 2)
  480.                => (1 . 2)
  481.      `cons' is often used to add a single element to the front of a
  482.      list.  This is called "consing the element onto the list".  For
  483.      example:
  484.           (setq list (cons newelt list))
  485.      Note that there is no conflict between the variable named `list'
  486.      used in this example and the function named `list' described below;
  487.      any symbol can serve both purposes.
  488.  - Function: list &rest OBJECTS
  489.      This function creates a list with OBJECTS as its elements.  The
  490.      resulting list is always `nil'-terminated.  If no OBJECTS are
  491.      given, the empty list is returned.
  492.           (list 1 2 3 4 5)
  493.                => (1 2 3 4 5)
  494.           (list 1 2 '(3 4 5) 'foo)
  495.                => (1 2 (3 4 5) foo)
  496.           (list)
  497.                => nil
  498.  - Function: make-list LENGTH OBJECT
  499.      This function creates a list of length LENGTH, in which all the
  500.      elements have the identical value OBJECT.  Compare `make-list'
  501.      with `make-string' (*note Creating Strings::.).
  502.           (make-list 3 'pigs)
  503.                => (pigs pigs pigs)
  504.           (make-list 0 'pigs)
  505.                => nil
  506.  - Function: append &rest SEQUENCES
  507.      This function returns a list containing all the elements of
  508.      SEQUENCES.  The SEQUENCES may be lists, vectors, or strings, but
  509.      the last one should be a list.  All arguments except the last one
  510.      are copied, so none of them are altered.
  511.      More generally, the final argument to `append' may be any Lisp
  512.      object.  The final argument is not copied or converted; it becomes
  513.      the CDR of the last cons cell in the new list.  If the final
  514.      argument is itself a list, then its elements become in effect
  515.      elements of the result list.  If the final element is not a list,
  516.      the result is a "dotted list" since its final CDR is not `nil' as
  517.      required in a true list.
  518.      See `nconc' in *Note Rearrangement::, for a way to join lists with
  519.      no copying.
  520.      Here is an example of using `append':
  521.           (setq trees '(pine oak))
  522.                => (pine oak)
  523.           (setq more-trees (append '(maple birch) trees))
  524.                => (maple birch pine oak)
  525.           
  526.           trees
  527.                => (pine oak)
  528.           more-trees
  529.                => (maple birch pine oak)
  530.           (eq trees (cdr (cdr more-trees)))
  531.                => t
  532.      You can see how `append' works by looking at a box diagram.  The
  533.      variable `trees' is set to the list `(pine oak)' and then the
  534.      variable `more-trees' is set to the list `(maple birch pine oak)'.
  535.      However, the variable `trees' continues to refer to the original
  536.      list:
  537.           more-trees                trees
  538.           |                           |
  539.           |     ___ ___      ___ ___   -> ___ ___      ___ ___
  540.            --> |___|___|--> |___|___|--> |___|___|--> |___|___|--> nil
  541.                  |            |            |            |
  542.                  |            |            |            |
  543.                   --> maple    -->birch     --> pine     --> oak
  544.      An empty sequence contributes nothing to the value returned by
  545.      `append'.  As a consequence of this, a final `nil' argument forces
  546.      a copy of the previous argument.
  547.           trees
  548.                => (pine oak)
  549.           (setq wood (append trees ()))
  550.                => (pine oak)
  551.           wood
  552.                => (pine oak)
  553.           (eq wood trees)
  554.                => nil
  555.      This once was the usual way to copy a list, before the function
  556.      `copy-sequence' was invented.  *Note Sequences Arrays Vectors::.
  557.      With the help of `apply', we can append all the lists in a list of
  558.      lists:
  559.           (apply 'append '((a b c) nil (x y z) nil))
  560.                => (a b c x y z)
  561.      If no SEQUENCES are given, `nil' is returned:
  562.           (append)
  563.                => nil
  564.      Here are some examples where the final argument is not a list:
  565.           (append '(x y) 'z)
  566.                => (x y . z)
  567.           (append '(x y) [z])
  568.                => (x y . [z])
  569.      The second example shows that when the final argument is a
  570.      sequence but not a list, the sequence's elements do not become
  571.      elements of the resulting list.  Instead, the sequence becomes the
  572.      final CDR, like any other non-list final argument.
  573.      The `append' function also allows integers as arguments.  It
  574.      converts them to strings of digits, making up the decimal print
  575.      representation of the integer, and then uses the strings instead
  576.      of the original integers.  *Don't use this feature; we plan to
  577.      eliminate it.  If you already use this feature, change your
  578.      programs now!*  The proper way to convert an integer to a decimal
  579.      number in this way is with `format' (*note Formatting Strings::.)
  580.      or `number-to-string' (*note String Conversion::.).
  581.  - Function: reverse LIST
  582.      This function creates a new list whose elements are the elements of
  583.      LIST, but in reverse order.  The original argument LIST is *not*
  584.      altered.
  585.           (setq x '(1 2 3 4))
  586.                => (1 2 3 4)
  587.           (reverse x)
  588.                => (4 3 2 1)
  589.           x
  590.                => (1 2 3 4)
  591. File: elisp,  Node: Modifying Lists,  Next: Sets And Lists,  Prev: Building Lists,  Up: Lists
  592. Modifying Existing List Structure
  593. =================================
  594.    You can modify the CAR and CDR contents of a cons cell with the
  595. primitives `setcar' and `setcdr'.
  596.      Common Lisp note: Common Lisp uses functions `rplaca' and `rplacd'
  597.      to alter list structure; they change structure the same way as
  598.      `setcar' and `setcdr', but the Common Lisp functions return the
  599.      cons cell while `setcar' and `setcdr' return the new CAR or CDR.
  600. * Menu:
  601. * Setcar::          Replacing an element in a list.
  602. * Setcdr::          Replacing part of the list backbone.
  603.                       This can be used to remove or add elements.
  604. * Rearrangement::   Reordering the elements in a list; combining lists.
  605. File: elisp,  Node: Setcar,  Next: Setcdr,  Up: Modifying Lists
  606. Altering List Elements with `setcar'
  607. ------------------------------------
  608.    Changing the CAR of a cons cell is done with `setcar'.  When used on
  609. a list, `setcar' replaces one element of a list with a different
  610. element.
  611.  - Function: setcar CONS OBJECT
  612.      This function stores OBJECT as the new CAR of CONS, replacing its
  613.      previous CAR.  It returns the value OBJECT.  For example:
  614.           (setq x '(1 2))
  615.                => (1 2)
  616.           (setcar x 4)
  617.                => 4
  618.           x
  619.                => (4 2)
  620.    When a cons cell is part of the shared structure of several lists,
  621. storing a new CAR into the cons changes one element of each of these
  622. lists.  Here is an example:
  623.      ;; Create two lists that are partly shared.
  624.      (setq x1 '(a b c))
  625.           => (a b c)
  626.      (setq x2 (cons 'z (cdr x1)))
  627.           => (z b c)
  628.      
  629.      ;; Replace the CAR of a shared link.
  630.      (setcar (cdr x1) 'foo)
  631.           => foo
  632.      x1                           ; Both lists are changed.
  633.           => (a foo c)
  634.      x2
  635.           => (z foo c)
  636.      
  637.      ;; Replace the CAR of a link that is not shared.
  638.      (setcar x1 'baz)
  639.           => baz
  640.      x1                           ; Only one list is changed.
  641.           => (baz foo c)
  642.      x2
  643.           => (z foo c)
  644.    Here is a graphical depiction of the shared structure of the two
  645. lists in the variables `x1' and `x2', showing why replacing `b' changes
  646. them both:
  647.              ___ ___        ___ ___      ___ ___
  648.      x1---> |___|___|----> |___|___|--> |___|___|--> nil
  649.               |        -->   |            |
  650.               |       |      |            |
  651.                --> a  |       --> b        --> c
  652.                       |
  653.             ___ ___   |
  654.      x2--> |___|___|--
  655.              |
  656.              |
  657.               --> z
  658.    Here is an alternative form of box diagram, showing the same
  659. relationship:
  660.      x1:
  661.       --------------       --------------       --------------
  662.      | car   | cdr  |     | car   | cdr  |     | car   | cdr  |
  663.      |   a   |   o------->|   b   |   o------->|   c   |  nil |
  664.      |       |      |  -->|       |      |     |       |      |
  665.       --------------  |    --------------       --------------
  666.                       |
  667.      x2:              |
  668.       --------------  |
  669.      | car   | cdr  | |
  670.      |   z   |   o----
  671.      |       |      |
  672.       --------------
  673. File: elisp,  Node: Setcdr,  Next: Rearrangement,  Prev: Setcar,  Up: Modifying Lists
  674. Altering the CDR of a List
  675. --------------------------
  676.    The lowest-level primitive for modifying a CDR is `setcdr':
  677.  - Function: setcdr CONS OBJECT
  678.      This function stores OBJECT as the new CDR of CONS, replacing its
  679.      previous CDR.  It returns the value OBJECT.
  680.    Here is an example of replacing the CDR of a list with a different
  681. list.  All but the first element of the list are removed in favor of a
  682. different sequence of elements.  The first element is unchanged,
  683. because it resides in the CAR of the list, and is not reached via the
  684.      (setq x '(1 2 3))
  685.           => (1 2 3)
  686.      (setcdr x '(4))
  687.           => (4)
  688.      x
  689.           => (1 4)
  690.    You can delete elements from the middle of a list by altering the
  691. CDRs of the cons cells in the list.  For example, here we delete the
  692. second element, `b', from the list `(a b c)', by changing the CDR of
  693. the first cell:
  694.      (setq x1 '(a b c))
  695.           => (a b c)
  696.      (setcdr x1 (cdr (cdr x1)))
  697.           => (c)
  698.      x1
  699.           => (a c)
  700.    Here is the result in box notation:
  701.                         --------------------
  702.                        |                    |
  703.       --------------   |   --------------   |    --------------
  704.      | car   | cdr  |  |  | car   | cdr  |   -->| car   | cdr  |
  705.      |   a   |   o-----   |   b   |   o-------->|   c   |  nil |
  706.      |       |      |     |       |      |      |       |      |
  707.       --------------       --------------        --------------
  708. The second cons cell, which previously held the element `b', still
  709. exists and its CAR is still `b', but it no longer forms part of this
  710. list.
  711.    It is equally easy to insert a new element by changing CDRs:
  712.      (setq x1 '(a b c))
  713.           => (a b c)
  714.      (setcdr x1 (cons 'd (cdr x1)))
  715.           => (d b c)
  716.      x1
  717.           => (a d b c)
  718.    Here is this result in box notation:
  719.      --------------        -------------       -------------
  720.      | car  | cdr   |      | car  | cdr  |     | car  | cdr  |
  721.      |   a  |   o   |   -->|   b  |   o------->|   c  |  nil |
  722.      |      |   |   |  |   |      |      |     |      |      |
  723.       --------- | --   |    -------------       -------------
  724.                 |      |
  725.           -----         --------
  726.          |                      |
  727.          |    ---------------   |
  728.          |   | car   | cdr   |  |
  729.           -->|   d   |   o------
  730.              |       |       |
  731.               ---------------
  732. File: elisp,  Node: Rearrangement,  Prev: Setcdr,  Up: Modifying Lists
  733. Functions that Rearrange Lists
  734. ------------------------------
  735.    Here are some functions that rearrange lists "destructively" by
  736. modifying the CDRs of their component cons cells.  We call these
  737. functions "destructive" because they chew up the original lists passed
  738. to them as arguments, to produce a new list that is the returned value.
  739.    See `delq', in *Note Sets And Lists::, for another function that
  740. modifies cons cells.
  741.  - Function: nconc &rest LISTS
  742.      This function returns a list containing all the elements of LISTS.
  743.      Unlike `append' (*note Building Lists::.), the LISTS are *not*
  744.      copied.  Instead, the last CDR of each of the LISTS is changed to
  745.      refer to the following list.  The last of the LISTS is not
  746.      altered.  For example:
  747.           (setq x '(1 2 3))
  748.                => (1 2 3)
  749.           (nconc x '(4 5))
  750.                => (1 2 3 4 5)
  751.           x
  752.                => (1 2 3 4 5)
  753.      Since the last argument of `nconc' is not itself modified, it is
  754.      reasonable to use a constant list, such as `'(4 5)', as in the
  755.      above example.  For the same reason, the last argument need not be
  756.      a list:
  757.           (setq x '(1 2 3))
  758.                => (1 2 3)
  759.           (nconc x 'z)
  760.                => (1 2 3 . z)
  761.           x
  762.                => (1 2 3 . z)
  763.      A common pitfall is to use a quoted constant list as a non-last
  764.      argument to `nconc'.  If you do this, your program will change
  765.      each time you run it!  Here is what happens:
  766.           (defun add-foo (x)            ; We want this function to add
  767.             (nconc '(foo) x))           ;   `foo' to the front of its arg.
  768.           (symbol-function 'add-foo)
  769.                => (lambda (x) (nconc (quote (foo)) x))
  770.           (setq xx (add-foo '(1 2)))    ; It seems to work.
  771.                => (foo 1 2)
  772.           (setq xy (add-foo '(3 4)))    ; What happened?
  773.                => (foo 1 2 3 4)
  774.           (eq xx xy)
  775.                => t
  776.           (symbol-function 'add-foo)
  777.                => (lambda (x) (nconc (quote (foo 1 2 3 4) x)))
  778.  - Function: nreverse LIST
  779.      This function reverses the order of the elements of LIST.  Unlike
  780.      `reverse', `nreverse' alters its argument by reversing the CDRs in
  781.      the cons cells forming the list.  The cons cell that used to be
  782.      the last one in LIST becomes the first cell of the value.
  783.      For example:
  784.           (setq x '(1 2 3 4))
  785.                => (1 2 3 4)
  786.           x
  787.                => (1 2 3 4)
  788.           (nreverse x)
  789.                => (4 3 2 1)
  790.           ;; The cell that was first is now last.
  791.           x
  792.                => (1)
  793.      To avoid confusion, we usually store the result of `nreverse' back
  794.      in the same variable which held the original list:
  795.           (setq x (nreverse x))
  796.      Here is the `nreverse' of our favorite example, `(a b c)',
  797.      presented graphically:
  798.           Original list head:                       Reversed list:
  799.            -------------        -------------        ------------
  800.           | car  | cdr  |      | car  | cdr  |      | car | cdr  |
  801.           |   a  |  nil |<--   |   b  |   o  |<--   |   c |   o  |
  802.           |      |      |   |  |      |   |  |   |  |     |   |  |
  803.            -------------    |   --------- | -    |   -------- | -
  804.                             |             |      |            |
  805.                              -------------        ------------
  806.  - Function: sort LIST PREDICATE
  807.      This function sorts LIST stably, though destructively, and returns
  808.      the sorted list.  It compares elements using PREDICATE.  A stable
  809.      sort is one in which elements with equal sort keys maintain their
  810.      relative order before and after the sort.  Stability is important
  811.      when successive sorts are used to order elements according to
  812.      different criteria.
  813.      The argument PREDICATE must be a function that accepts two
  814.      arguments.  It is called with two elements of LIST.  To get an
  815.      increasing order sort, the PREDICATE should return `t' if the
  816.      first element is "less than" the second, or `nil' if not.
  817.      The destructive aspect of `sort' is that it rearranges the cons
  818.      cells forming LIST by changing CDRs.  A nondestructive sort
  819.      function would create new cons cells to store the elements in their
  820.      sorted order.  If you wish to make a sorted copy without
  821.      destroying the original, copy it first with `copy-sequence' and
  822.      then sort.
  823.      Sorting does not change the CARs of the cons cells in LIST; the
  824.      cons cell that originally contained the element `a' in LIST still
  825.      has `a' in its CAR after sorting, but it now appears in a
  826.      different position in the list due to the change of CDRs.  For
  827.      example:
  828.           (setq nums '(1 3 2 6 5 4 0))
  829.                => (1 3 2 6 5 4 0)
  830.           (sort nums '<)
  831.                => (0 1 2 3 4 5 6)
  832.           nums
  833.                => (1 2 3 4 5 6)
  834.      Note that the list in `nums' no longer contains 0; this is the same
  835.      cons cell that it was before, but it is no longer the first one in
  836.      the list.  Don't assume a variable that formerly held the argument
  837.      now holds the entire sorted list!  Instead, save the result of
  838.      `sort' and use that.  Most often we store the result back into the
  839.      variable that held the original list:
  840.           (setq nums (sort nums '<))
  841.      *Note Sorting::, for more functions that perform sorting.  See
  842.      `documentation' in *Note Accessing Documentation::, for a useful
  843.      example of `sort'.
  844. File: elisp,  Node: Sets And Lists,  Next: Association Lists,  Prev: Modifying Lists,  Up: Lists
  845. Using Lists as Sets
  846. ===================
  847.    A list can represent an unordered mathematical set--simply consider a
  848. value an element of a set if it appears in the list, and ignore the
  849. order of the list.  To form the union of two sets, use `append' (as
  850. long as you don't mind having duplicate elements).  Other useful
  851. functions for sets include `memq' and `delq', and their `equal'
  852. versions, `member' and `delete'.
  853.      Common Lisp note: Common Lisp has functions `union' (which avoids
  854.      duplicate elements) and `intersection' for set operations, but GNU
  855.      Emacs Lisp does not have them.  You can write them in Lisp if you
  856.      wish.
  857.  - Function: memq OBJECT LIST
  858.      This function tests to see whether OBJECT is a member of LIST.  If
  859.      it is, `memq' returns a list starting with the first occurrence of
  860.      OBJECT.  Otherwise, it returns `nil'.  The letter `q' in `memq'
  861.      says that it uses `eq' to compare OBJECT against the elements of
  862.      the list.  For example:
  863.           (memq 'b '(a b c b a))
  864.                => (b c b a)
  865.           (memq '(2) '((1) (2)))    ; `(2)' and `(2)' are not `eq'.
  866.                => nil
  867.  - Function: delq OBJECT LIST
  868.      This function destructively removes all elements `eq' to OBJECT
  869.      from LIST.  The letter `q' in `delq' says that it uses `eq' to
  870.      compare OBJECT against the elements of the list, like `memq'.
  871.    When `delq' deletes elements from the front of the list, it does so
  872. simply by advancing down the list and returning a sublist that starts
  873. after those elements:
  874.      (delq 'a '(a b c)) == (cdr '(a b c))
  875.    When an element to be deleted appears in the middle of the list,
  876. removing it involves changing the CDRs (*note Setcdr::.).
  877.      (setq sample-list '(a b c (4)))
  878.           => (a b c (4))
  879.      (delq 'a sample-list)
  880.           => (b c (4))
  881.      sample-list
  882.           => (a b c (4))
  883.      (delq 'c sample-list)
  884.           => (a b (4))
  885.      sample-list
  886.           => (a b (4))
  887.    Note that `(delq 'c sample-list)' modifies `sample-list' to splice
  888. out the third element, but `(delq 'a sample-list)' does not splice
  889. anything--it just returns a shorter list.  Don't assume that a variable
  890. which formerly held the argument LIST now has fewer elements, or that
  891. it still holds the original list!  Instead, save the result of `delq'
  892. and use that.  Most often we store the result back into the variable
  893. that held the original list:
  894.      (setq flowers (delq 'rose flowers))
  895.    In the following example, the `(4)' that `delq' attempts to match
  896. and the `(4)' in the `sample-list' are not `eq':
  897.      (delq '(4) sample-list)
  898.           => (a c (4))
  899.    The following two functions are like `memq' and `delq' but use
  900. `equal' rather than `eq' to compare elements.  They are new in Emacs 19.
  901.  - Function: member OBJECT LIST
  902.      The function `member' tests to see whether OBJECT is a member of
  903.      LIST, comparing members with OBJECT using `equal'.  If OBJECT is a
  904.      member, `member' returns a list starting with its first occurrence
  905.      in LIST.  Otherwise, it returns `nil'.
  906.      Compare this with `memq':
  907.           (member '(2) '((1) (2)))  ; `(2)' and `(2)' are `equal'.
  908.                => ((2))
  909.           (memq '(2) '((1) (2)))    ; `(2)' and `(2)' are not `eq'.
  910.                => nil
  911.           ;; Two strings with the same contents are `equal'.
  912.           (member "foo" '("foo" "bar"))
  913.                => ("foo" "bar")
  914.  - Function: delete OBJECT LIST
  915.      This function destructively removes all elements `equal' to OBJECT
  916.      from LIST.  It is to `delq' as `member' is to `memq': it uses
  917.      `equal' to compare elements with OBJECT, like `member'; when it
  918.      finds an element that matches, it removes the element just as
  919.      `delq' would.  For example:
  920.           (delete '(2) '((2) (1) (2)))
  921.                => ((1))
  922.      Common Lisp note: The functions `member' and `delete' in GNU Emacs
  923.      Lisp are derived from Maclisp, not Common Lisp.  The Common Lisp
  924.      versions do not use `equal' to compare elements.
  925.    See also the function `add-to-list', in *Note Setting Variables::,
  926. for another way to add an element to a list stored in a variable.
  927.